<?php
/**
 * Created by JieHuiLong.
 * E-mail: jiehuilong@icloud.com
 * Date: 2016/8/16
 * Time: 16:00
 * 广告分组模型
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Models\Wechat;
use App\Models\Domain;

class Group extends Model
{
    protected $connection="mysql2";//指定数据库
    public function getConfig($gid,$did,$company =false)
    {
        $obj = $this->where('id',$gid)->first();

        $isGeneralize = Domain::where('id',$did)->value('status');

        $str = "<?php \n";

        $str .= "//是否打开推广：0-关闭推广 1-打开推广 \n";
        $str .= '$isGeneralize='.$isGeneralize."; \n";

        $str .= "//跳转类型：0-不跳转 1-手机端 2-全部跳转 \n";
        $str .= '$go_to_type='.$obj->go_to_type."; \n";

        $str .= "//ip限制 \n";
        $obj->ips = str_replace('*','.',$obj->ips);
        $str .= '$ips='.$obj->ips."; \n";

        $str .= "//投放时间段 \n";
        $str .= '$delivery_time="'.$obj->delivery_time."\"; \n";

        $jumpList = Domain::where('gid',$obj->target_gid)->where('status',1)->select('domain','directory')->get();
        $str .= "//跳转分组列表 \n";
        $str.='$jumpList=array(';
        $outJumpList= "";
        foreach ($jumpList as $keyJL => $objJL){
            if($keyJL > 0) $outJumpList .=",";
            $outJumpList .= '"'.$objJL->domain.'/'.$objJL->directory.  '"';
        }
        $str .= $outJumpList;
        $str.=");\n";

        $str .= "//跳转目标[分组ID] \n";
        $str .= '$target_gid='.$obj->target_gid."; \n";

        $str .= "//渠道限制 null-不限制 \n";
        $str .= '$channel="'.trim($obj->channel)."\"; \n";

        $str .= "//区域过滤：0-关闭，2：开启 \n";
        $str .= '$area_config='.$obj->area_config."; \n";

        $str .= "//设置过滤地区（不投放区域） null-不限制\n";
        $str .= '$area_list="'.trim(preg_replace('/\d+\-/','',$obj->area_list))."\"; \n";

        $str .= "//设备类型：0-全部 ，1-移动，2-PC \n";
        $str .= '$device_type='.$obj->device_type."; \n";

        $str .= "//站点类型 \n";
        $str .= '$station_type='.$obj->station_type."; \n";

        $str.="//Start---配置微信号";
        $str.= "\n //微信号，微信昵称，默认随机值，随机值剩余次数，客服上线时间，客服下线时间 \n";
        if(empty($obj->wechat_ids)){
            $wechatList = Wechat::whereIn('wx_num',explode(',',$obj->wechat_list))->where('status',1)
                ->select('wx_num','wx_nike','max_rand','status','online_time','offline_time')
                ->get()->toArray();
        }else{
            $wechatList = Wechat::whereIn('id',explode(',',$obj->wechat_ids))->where('status',1)
                ->select('wx_num','wx_nike','max_rand','status','online_time','offline_time')
                ->get()->toArray();
        }
        $str.='$wechatList=['."\n";
        if(count($wechatList) != 0){
            foreach ($wechatList as $key => $obj) {
                $obj = '["' . str_replace(',', '","', join(',', $obj)) . "\"],\n";
                $str.= $obj;
            }
        }else{
            $str.='["DefautWechat", "LServer", "5", "50", "00:00", "23:59"],';
        }


        $str.="]; \n";
        $str.="//End---配置微信号 \n";

        $str .= "//引入判断文件 \n";
        if($company){
            $str .= 'include($_SERVER["DOCUMENT_ROOT"]."/judgeCompany.php")'."; \n";
        }else{
            $str .= 'include($_SERVER["DOCUMENT_ROOT"]."/judge.php")'."; \n";
        }

        $str .= "?> \n";
        return $str;
    }
}
